package dao.fachada;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import models.Produto;
import models.Tag;

import dao.ConexaoBanco;
import dao.FachadaDAO;
import dao.ProdutoDAO;
import dao.TagDAO;

public class FachadaTagDAO {
	
	private static FachadaTagDAO singleton = null;
	
	private java.sql.Connection conexao;
	private ConexaoBanco conexaoBanco;
	
	public static FachadaTagDAO getInstance() {
        if (singleton == null) {
	        // "lazy instantiation"
	        singleton = new FachadaTagDAO();
        }
        return singleton;
    }
	
	private Connection openConexaoBanco(){
		this.conexaoBanco = new ConexaoBanco();		
		this.conexao = this.conexaoBanco.openConexao();
	
		return this.conexao;
	}
	
	public Tag buscarTagPorNome(String nome){
		TagDAO tagDAO = new TagDAO(this.openConexaoBanco());
		return tagDAO.buscarTagPorNome(nome);
	}
	
	public Tag buscarTagPorId(String id){
		this.openConexaoBanco();
		
		Tag tag = new Tag();
		
		tag.setId(Integer.parseInt(id));
		
		try {
			String comando = "select * from tag where id='"+id+"';";
			java.sql.Statement start = conexao.createStatement();
			ResultSet rs = start.executeQuery(comando);
			
			if(rs.first()){
				tag.setNome(rs.getString("nome"));
			}
		} catch (Exception e) {

		}
		return tag;
	}
	
	public void alterar(Tag tag) throws SQLException{
		String comandoAlterar = "UPDATE tag SET  nome = ? WHERE  tag.id = ?;";
	
		PreparedStatement p = this.conexao.prepareStatement(comandoAlterar);
		
		p.setString(1, tag.getNome());
		p.setInt(2, tag.getId());
		
		p.execute();
		
		return;
	}

}
